//
//  DestinationViewController.m
//  CommonFrame
//
//  Created by Bang-guo on 15/7/15.
//  Copyright (c) 2015年 guobang. All rights reserved.
//

#import "DestinationViewController.h"


@interface DestinationViewController ()

@end

@implementation DestinationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"目的地";
    self.view.backgroundColor = [UIColor redColor];
    
    self.sectionArray = [NSMutableArray array];
    self.allArray = [NSMutableArray array];
    [self getDataValue];
    
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    //设置item大小
    flowLayout.itemSize = CGSizeMake((WIDTH - 45) / 2, (WIDTH - 45) / 2);
    //头部区域视图大小
    flowLayout.headerReferenceSize = CGSizeMake(WIDTH, 30);
    
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    //距离边界的间距 上 左 下 右
    flowLayout.sectionInset = UIEdgeInsetsMake(15, 14, 15, 14);
    //列间距
    flowLayout.minimumInteritemSpacing = 15;
    //行间距
    flowLayout.minimumLineSpacing = 15;
    
    self.destinationCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 113) collectionViewLayout:flowLayout];
    self.destinationCollectionView.delegate = self;
    self.destinationCollectionView.dataSource = self;
    self.destinationCollectionView.backgroundColor = [UIColor whiteColor];
    //注册方法
    [self.destinationCollectionView registerClass:[DestinationCollectionViewCell class] forCellWithReuseIdentifier:@"reuse"];
    //注册头部区域
//    [self.destinationCollectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerReuse"];
    
    [self.view addSubview:self.destinationCollectionView];
    //让菊花旋转起来
    self.HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    self.HUD.labelText = @"正在加载中...";
    [self.HUD show:YES];


}

-(void)getDataValue {
    
    NSString *str_url = [NSString stringWithFormat:@"http://api.breadtrip.com/destination/v3/?last_modified_since=0"];
    NSMutableArray *tempArray = [NSMutableArray array];
    
    [AFNGetDataValue getDataAFN:str_url block:^(NSMutableDictionary *dataDic) {
        

        NSMutableArray *array = [NSMutableArray arrayWithArray:[dataDic objectForKey:@"elements"]];
        for (NSMutableDictionary *dic in array) {
            Destination *des = [[Destination alloc] init];
            des.index = [NSString stringWithFormat:@"%@", [dic objectForKey:@"index"]];
            des.title = [dic objectForKey:@"title"];
            [self.sectionArray addObject:des];
            NSMutableArray *arr = [NSMutableArray arrayWithArray:[dic objectForKey:@"data"]];
            for (NSMutableDictionary *dic in arr) {
                
                Destination *des1 = [[Destination alloc] init];
                [des1 setValuesForKeysWithDictionary:dic];
                des1.placeId = [NSString stringWithFormat:@"%@", [dic objectForKey:@"id"]];
                des1.type = [NSString stringWithFormat:@"%@", [dic objectForKey:@"type"]];
                [tempArray addObject:des1];
            }
            
            
        }
        
        NSMutableArray *array0 = [NSMutableArray array];
        NSMutableArray *array1 = [NSMutableArray array];
        NSMutableArray *array2 = [NSMutableArray array];
        NSMutableArray *array3 = [NSMutableArray array];
        NSMutableArray *array4 = [NSMutableArray array];
        NSMutableArray *array5 = [NSMutableArray array];
        NSMutableArray *array6 = [NSMutableArray array];
        
        for (int i = 0; i < tempArray.count; i++) {
            
            if (i >= 0 && i <= 3) {
                Destination *des = [[Destination alloc] init];
                
                des = tempArray[i];
                [array0 addObject:des];
            }
            if (i >= 4 && i <= 7) {
                Destination *des = [[Destination alloc] init];
                
                des = tempArray[i];
                [array1 addObject:des];
            }
            if (i >= 8 && i <= 11) {
                Destination *des = [[Destination alloc] init];
                
                des = tempArray[i];
                [array2 addObject:des];
            }
            if (i >= 12 && i <= 15) {
                Destination *des = [[Destination alloc] init];
                
                des = tempArray[i];
                [array3 addObject:des];
            }
            if (i >= 16 && i <= 17) {
                Destination *des = [[Destination alloc] init];
                
                des = tempArray[i];
                [array4 addObject:des];
            }
            if (i >= 18 && i <= 21) {
                Destination *des = [[Destination alloc] init];
                
                des = tempArray[i];
                [array5 addObject:des];
            }
            if (i >= 22 && i <= 25) {
                Destination *des = [[Destination alloc] init];
                
                des = tempArray[i];
                [array6 addObject:des];
            }
            
            
        }
        
        [self.allArray addObject:array0];
        [self.allArray addObject:array1];
        [self.allArray addObject:array2];
        [self.allArray addObject:array3];
        [self.allArray addObject:array4];
        [self.allArray addObject:array5];
        [self.allArray addObject:array6];
        [self.destinationCollectionView reloadData];
        [self.HUD hide:YES];
    }];
    
}

-(void)tapAction:(UITapGestureRecognizer *)tap {
    
    UILabel *lable = (UILabel *)tap.view;
    NSLog(@"%ld", lable.tag);
    DestinationMoreViewController *destionationMoreVC = [[DestinationMoreViewController alloc] init];
    Destination *des = self.sectionArray[lable.tag - 10000];
    destionationMoreVC.des = des;
    [self.navigationController pushViewController:destionationMoreVC animated:YES];
    
    
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    

    
    DestinationDetailViewController *destinationD = [[DestinationDetailViewController alloc] init];
    NearbyDetailViewController *nearbyDVC = [[NearbyDetailViewController alloc] init];
        Destination *des = self.allArray[indexPath.section][indexPath.row];
//    NSLog(@"%@", des.type);
    if ([des.type isEqualToString:@"1"] || [des.type isEqualToString:@"3"]) {
        destinationD.des = des;
        [self.navigationController pushViewController:destinationD animated:YES];
        
    } else {
       
        nearbyDVC.placeId = des.placeId;
        [self.navigationController pushViewController:nearbyDVC animated:YES];
    }
    
    

    
    
    
}

//设置表头和底部区域内容
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    NSString *headReuse = [NSString stringWithFormat:@"head%ld", indexPath.section];
    
    
    [self.destinationCollectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headReuse];
    //kind是显示区域类型
    if (kind == UICollectionElementKindSectionHeader) {
        UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:headReuse forIndexPath:indexPath];
//        headerView.backgroundColor = [UIColor greenColor];
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 0, WIDTH - 20, 30)];
        if (self.sectionArray.count != 0) {
            
            Destination *des = self.sectionArray[indexPath.section];
            label.text = des.title;
            
        }
        
        [headerView addSubview:label];

        if (indexPath.section == 2 || indexPath.section == 5 || indexPath.section == 6) {
            
            UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, WIDTH - 40, 30)];
            label1.text = @">";
            label1.textColor = [UIColor blackColor];
            label1.textAlignment = NSTextAlignmentRight;
            
            label.tag = 10000 + indexPath.section;
            [label addSubview:label1];
            
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
            label.userInteractionEnabled = YES;
            [label addGestureRecognizer:tap];
            
        }
        
        return headerView;

    
    } else {
        
        return nil;
    }
    
    
}



//设置多个分区个数
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 7;
}




//设置item的个数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    

    if (section == 4) {
        
        return 2;
    }
    
    return 4;
}

//布局item
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    
    //直接从系统的重用池里取ite
    DestinationCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];


    if (self.allArray.count != 0) {
        NSMutableArray *array = self.allArray[indexPath.section];
        if (array.count != 0) {
            
            Destination *des = array[indexPath.row];
            cell.des = des;
            
        }
        
        
    }
    
    return cell;
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
